﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class VehiScheduleEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.QueryString["id"];
                if (!string.IsNullOrEmpty(id))
                {
                    ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
                    ReqVehicleSubs reqVehicleSubs = ctx.ReqVehicleSubs.Single(r => r.Id == int.Parse(id));
                    FromSite.Value = reqVehicleSubs.FromSite;
                    ToSite.Value = reqVehicleSubs.ToSite;
                    PreDistance.Value = reqVehicleSubs.PreDistance.ToString();
                    PreTimespan.Value = reqVehicleSubs.PreTimespan.ToString();
                    Cause.Value = reqVehicleSubs.Cause;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
            ReqVehicleSubs reqVehicleSubs;
            string id = Request.QueryString["id"];
            if (string.IsNullOrEmpty(id))
            {
                reqVehicleSubs = new ReqVehicleSubs();
                reqVehicleSubs.FormNo = Request.QueryString["formNo"];
                ctx.ReqVehicleSubs.InsertOnSubmit(reqVehicleSubs);
            }
            else
            {
                reqVehicleSubs = ctx.ReqVehicleSubs.Single(r => r.Id == int.Parse(id));
            }
            reqVehicleSubs.FromSite = FromSite.Value;
            reqVehicleSubs.ToSite = ToSite.Value;
            reqVehicleSubs.PreDistance = int.Parse(PreDistance.Value);
            reqVehicleSubs.PreTimespan =int.Parse(PreTimespan.Value);
            reqVehicleSubs.Cause = Cause.Value;
            ctx.SubmitChanges();
            Response.Write("<script>window.returnValue='Succeed';window.close();</script>");
        }
    }
}